/**
 * create：2020年2月26日
 */
const db = require('../tools/mongodb');
const config = require('./mongodb.config');
const agent = require('./agent');
const delay = require('../tools/delay');
const format = require('../tools/format');
format();
const waitMillis = 10000;
const randomMillis = function () {
    return Math.random() * 15000;
};
const dateFormat = "yyyy-MM-dd HH:mm:ss";
const pageOut = 200;

const fs = require('fs');

let fn = (async () => {
    let tmall = config.tmall;
    let startTime = new Date().format(dateFormat);
    let errList = [];
    try {
        let summarys = [];
        await db.find(tmall.summary, { 'disabled': { $ne: "1" } }).then(r => { summarys = r; }).catch(error => { throw error })
        for (let sum of summarys) {
            try {
                let commentIdList = [];//记录id，发现重复跳出
                //step 1 获取商品信息
                let _new = {};
                await agent.getCommentVersion(sum.itemId).then(r => { _new = r }).catch(error => { throw error });
                //step 2 获取首页(评论数)
                let _rateConfig = _new.rateConfig;
                _rateConfig.currentPage = 1;
                let firstPage = {};
                let diff = 0;
                await agent.getCommentDetail(_rateConfig).then(r => {
                    if (!r.rateDetail || !r.rateDetail.rateList) {
                        fs.writeFileSync('./needLogin.json',JSON.stringify(r));
                        errList.push({
                            itemId: sum.itemId,
                            response: r
                        });
                        throw null;
                    }
                    firstPage = r;
                }).catch(error => { throw error });
                //如果最新评论数比已有评论数多
                diff = parseInt(firstPage.rateDetail.rateCount.total) - parseInt(sum.rateCount.total);
                if (diff > 0) {
                    let crawl = 0;
                    let insArrayFP = [];
                    //处理首页
                    for (let rate of firstPage.rateDetail.rateList) {
                        if (crawl < diff) {
                            insArrayFP.push({
                                id: rate.id,
                                rateDate: rate.rateDate,
                                cmsSource: rate.cmsSource,
                                buyCount: rate.buyCount,
                                auctionSku: rate.auctionSku,
                                displayUserNick: rate.displayUserNick,
                                auctionSku: rate.auctionSku,
                                rateContent: rate.rateContent,
                                pics: rate.pics,
                                videoList: rate.videoList,
                                reply: rate.reply,
                                useful: rate.useful,
                                tradeEndTime: new Date(Number(rate.tradeEndTime.time)).format(dateFormat),
                                gmtCreateTime: new Date(Number(rate.gmtCreateTime.time)).format(dateFormat),
                                create: new Date().format(dateFormat)
                            });
                            crawl++;
                        }
                        await db.insertMany(tmall.comments, insArrayFP).catch(error => { throw (`首页评论写入错误`) });
                        //其他页(超页数跳出避免死循环)
                        await delay(waitMillis);
                        while (crawl < diff || _rateConfig.currentPage >= pageOut) {
                            await delay(randomMillis());
                            _rateConfig.currentPage++;
                            let page = {};
                            let insArray = [];
                            await agent.getCommentDetail(_rateConfig).then(r => {
                                if (!r.rateDetail || !r.rateDetail.rateList) {
                                    fs.writeFileSync('./needLogin.json',JSON.stringify(r));
                                    errList.push({
                                        itemId: sum.itemId,
                                        response: r
                                    });
                                    throw null;
                                }
                                page = r;
                            }).catch(error => { throw error });
                            for (let rate of page.rateDetail.rateList) {
                                if (crawl < diff && !commentIdList.includes(rate.id)) {
                                    insArray.push({
                                        id: rate.id,
                                        rateDate: rate.rateDate,
                                        cmsSource: rate.cmsSource,
                                        buyCount: rate.buyCount,
                                        auctionSku: rate.auctionSku,
                                        displayUserNick: rate.displayUserNick,
                                        auctionSku: rate.auctionSku,
                                        rateContent: rate.rateContent,
                                        pics: rate.pics,
                                        videoList: rate.videoList,
                                        reply: rate.reply,
                                        useful: rate.useful,
                                        tradeEndTime: new Date(Number(rate.tradeEndTime.time)).format(dateFormat),
                                        gmtCreateTime: new Date(Number(rate.gmtCreateTime.time)).format(dateFormat),
                                        create: new Date().format(dateFormat)
                                    });
                                    commentIdList.push(rate.id);
                                    crawl++;
                                }
                            }
                            await db.insertMany(tmall.comments, insArray).catch(error => { throw (`第${_rateConfig.currentPage}页评论写入错误`) });
                        }
                    }
                }
            }
            catch (error) {
                if (error) {
                    console.error(error);
                    errList.push({
                        itemId: sum.itemId,
                        response: error
                    });
                }
                else{
                    throw error;
                }
                continue;
            }
            _new.changeTime = new Date().format(dateFormat);
            _new.rateCount = firstPage.rateCount;
            await db.updateMany(tmall.summary,
                { $and: [{ 'itemId': sum.itemId }, { 'disabled': { $ne: '1' } }] },
                _new
            ).catch(error => { throw error; })
        }
        await db.insertOne(tmall.log, {
            type: "normal",
            operation: "crawl",
            start: startTime,
            end: new Date().format(dateFormat),
            errList: errList
        });
    }
    catch (error) {
        console.error(error);
        await db.insertOne(tmall.log, {
            type: "error",
            operation: "crawl",
            start: startTime,
            end: new Date().format(dateFormat),
            message: error.message,
            stack: error.stack,
            errList: errList
        });
    }
});
module.exports = fn;
